import {createRouter, createWebHistory} from "vue-router";

// 本地静态路由
export const constantRoutes: any[] = [
    {
        path: '/',
        redirect: '/home/system/user'
    },
    {
        path: '/home',
        component: () => import('@/view/home/Index.vue'),
        children: [
            {
                path: 'system',
                component: () => import('@/view/home/system/Index.vue'),
                children: [
                    {
                        path: 'user',
                        component: () => import('@/view/home/system/user/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/system/user/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/system/user/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'chart',
                        component: () => import('@/view/home/system/chart/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/system/chart/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/system/chart/Add.vue')
                            }
                        ]
                    }
                ]
            },
            {
                path: 'partner',
                component: () => import('@/view/home/partner/Index.vue'),
                children: [
                    {
                        path: 'investors',
                        component: () => import('@/view/home/partner/investors/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/partner/investors/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/partner/investors/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'contracts',
                        component: () => import('@/view/home/partner/contracts/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/partner/contracts/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/partner/contracts/Add.vue')
                            }
                        ]
                    }
                ]
            },
            {
                path: 'operate',
                component: () => import('@/view/home/operate/Index.vue'),
                children: [
                    {
                        path: 'customers',
                        component: () => import('@/view/home/operate/customers/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/operate/customers/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/operate/customers/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'coupons',
                        component: () => import('@/view/home/operate/coupons/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/operate/coupons/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/operate/coupons/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'promotions',
                        component: () => import('@/view/home/operate/promotions/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/operate/promotions/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/operate/promotions/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'invitations',
                        component: () => import('@/view/home/operate/invitations/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/operate/invitations/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/operate/invitations/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'block',
                        component: () => import('@/view/home/operate/block/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/operate/block/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/operate/block/Add.vue')
                            }
                        ]
                    }
                ]
            },
            {
                path: 'station',
                component: () => import('@/view/home/station/Index.vue'),
                children: [
                    {
                        path: 'chargingStations',
                        component: () => import('@/view/home/station/chargingStations/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/station/chargingStations/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/station/chargingStations/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'chargingPiles',
                        component: () => import('@/view/home/station/chargingPiles/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/station/chargingPiles/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/station/chargingPiles/Add.vue')
                            }
                        ]
                    }
                ]
            },
            {
                path: 'transaction',
                component: () => import('@/view/home/transaction/Index.vue'),
                children: [
                    {
                        path: 'transactions',
                        component: () => import('@/view/home/transaction/transactions/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/transaction/transactions/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/transaction/transactions/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'records',
                        component: () => import('@/view/home/transaction/records/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/transaction/records/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/transaction/records/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'recharge',
                        component: () => import('@/view/home/transaction/recharge/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/transaction/recharge/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/transaction/recharge/Add.vue')
                            }
                        ]
                    }
                ]
            },
            {
                path: 'hazard',
                component: () => import('@/view/home/hazard/Index.vue'),
                children: [
                    {
                        path: 'hazards',
                        component: () => import('@/view/home/hazard/hazards/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/hazard/hazards/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/hazard/hazards/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'handle',
                        component: () => import('@/view/home/hazard/handle/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/hazard/handle/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/hazard/handle/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'faults',
                        component: () => import('@/view/home/hazard/faults/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/hazard/faults/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/hazard/faults/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'faultsHandle',
                        component: () => import('@/view/home/hazard/faultsHandle/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/hazard/faultsHandle/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/hazard/faultsHandle/Add.vue')
                            }
                        ]
                    }
                ]
            },
            {
                path: 'monitor',
                component: () => import('@/view/home/monitor/Index.vue'),
                children: [
                    {
                        path: 'video',
                        component: () => import('@/view/home/monitor/video/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/monitor/video/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/monitor/video/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'dashboard',
                        component: () => import('@/view/home/monitor/dashboard/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/monitor/dashboard/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/monitor/dashboard/Add.vue')
                            }
                        ]
                    }
                ]
            },
            {
                path: 'parameter',
                component: () => import('@/view/home/parameter/Index.vue'),
                children: [
                    {
                        path: 'fee',
                        component: () => import('@/view/home/parameter/fee/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/parameter/fee/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/parameter/fee/Add.vue')
                            }
                        ]
                    },
                    {
                        path: 'process',
                        component: () => import('@/view/home/parameter/process/Index.vue'),
                        children: [
                            {
                                path: 'list',
                                component: () => import('@/view/home/parameter/process/List.vue')
                            },
                            {
                                path: 'add',
                                component: () => import('@/view/home/parameter/process/Add.vue')
                            }
                        ]
                    }
                ]
            },
        ]
    },
    {
        path: '/login',
        component: () => import('@/view/login/Index.vue')
    },
    {
        path: '/register',
        component: () => import('@/view/register/Index.vue')
    }
];

// 创建路由
const router = createRouter({
    history: createWebHistory(),
    routes: constantRoutes,
});

router.beforeEach((to, from, next) => {
    if (to.path === '/login') {
        return next();
    }
    let appStore = localStorage.getItem("appStore")

    if (!appStore) {
        next('/login');
        return
    }

    let app = JSON.parse(appStore)

    if (!app.token) {
        next('/login');
        console.log(from.path)
        return
    }
    next()
});

export default router;